//
//  ShowInfoViewController.m
//  CloudMD
//
//  Created by apple on 16/3/2.
//  Copyright (c) 2016年 HuaZhengInfo. All rights reserved.
//

#import "ShowInfoViewController.h"

@interface ShowInfoViewController ()<UIScrollViewDelegate>{

    UIScrollView * _scrollerView;
    UIImageView * _imageView;
}

@end

@implementation ShowInfoViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    UIButton * leftButton = [GDFactory creatUIButtonWithFrame:CGRectMake(0, 0, 10, 15) backImage:@"left_arrow" title:nil titleLableColcor:nil titleLabelFont:nil];
    [leftButton addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    self.view.backgroundColor = [UIColor blackColor];
    _scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H)];
    _scrollerView.center = self.view.center;
    _scrollerView.showsHorizontalScrollIndicator = NO;
    _scrollerView.showsVerticalScrollIndicator = YES;
    _scrollerView.pagingEnabled = YES;
    //设置最大伸缩比例
    _scrollerView.maximumZoomScale=4.0;
    //设置最小伸缩比例
    _scrollerView.minimumZoomScale=0.5;

//    _imageArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"IMAGE"];
    if (_imageArray.count == 0) {
        
        _scrollerView.contentSize = CGSizeMake(SCR_W, 0);
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H)];
        UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
        _imageView.userInteractionEnabled = YES;
        [_imageView addGestureRecognizer:pinch];
        
        // 单机击手势
        UITapGestureRecognizer * tapImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
        tapImage.numberOfTapsRequired = 1;
        tapImage.numberOfTouchesRequired = 1;
        [_imageView addGestureRecognizer:tapImage];
        
        // 双击手势
        UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        //        doubleTap.numberOfTouchesRequired = 2;
        doubleTap.numberOfTapsRequired = 2;
        [tapImage requireGestureRecognizerToFail:doubleTap];
        [_imageView addGestureRecognizer:doubleTap];
        
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        _imageView.image = [UIImage imageNamed:@"placeholder"];
        [_scrollerView addSubview:_imageView];
    }else if (_imageArray.count > 0){
        
        _scrollerView.contentSize = CGSizeMake(SCR_W * _imageArray.count, 0);
        for (int i = 0; i < _imageArray.count; i ++) {
            
            NSString * str = _imageArray[i];
            NSURL * urlStr = [NSURL URLWithString:str];
            _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCR_W * i, 0, SCR_W, SCR_H)];
            // 捏合手势
            UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
            _imageView.userInteractionEnabled = YES;
//            [_imageView addGestureRecognizer:pinch];
            
            // 单机击手势
            UITapGestureRecognizer * tapImage = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
            tapImage.numberOfTapsRequired = 1;
            tapImage.numberOfTouchesRequired = 1;
//            [_imageView addGestureRecognizer:tapImage];
            
            // 双击手势
            UITapGestureRecognizer * doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
            //        doubleTap.numberOfTouchesRequired = 2;
            doubleTap.numberOfTapsRequired = 2;
            [tapImage requireGestureRecognizerToFail:doubleTap];
//            [_imageView addGestureRecognizer:doubleTap];
            _imageView.contentMode = UIViewContentModeScaleAspectFit;
            [_imageView setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"placeholder (1)"]];
            [_scrollerView addSubview:_imageView];
        }
    }
    
    [self.view addSubview:_scrollerView];
    
    
}

- (void)doubleTap:(UITapGestureRecognizer *)sender{
    
    UIImageView * imageView = (UIImageView *)sender.view;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        [imageView setTransform:CGAffineTransformScale(imageView.transform, 2.0, 2.0)];
    }];
    
}

- (void)tapImage:(UITapGestureRecognizer *)sender{
    
    UIImageView * imageView = (UIImageView *)sender.view;
    
    [UIView animateWithDuration:0.2 animations:^{
        
        [imageView setTransform:CGAffineTransformScale(imageView.transform, 0.5, 0.5)];
    }];
}

- (void)pinch:(UIPinchGestureRecognizer *)recongnizer{
    
    if (recongnizer.state == UIGestureRecognizerStateChanged) {
        
        [recongnizer.view setTransform:CGAffineTransformScale(recongnizer.view.transform, recongnizer.scale, recongnizer.scale)];
        recongnizer.scale = 1.0f;
    }
}

- (void)clickLeft {
    [self.navigationController popViewControllerAnimated:YES];
}

//- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
//
//    return _imageView;
//}

@end

